var http = require('http')
var fs = require('fs')
var url = require('url')
var port = process.argv[2]

if(!port){
    console.log('请指定端口号好不啦？\nnode server.js 8888 这样不会吗？')
    process.exit(1)
}

var server = http.createServer(function(request, response){
    var parsedUrl = url.parse(request.url, true)
    var pathWithQuery = request.url
    var queryString = ''
    if(pathWithQuery.indexOf('?') >= 0){ queryString = pathWithQuery.substring(pathWithQuery.indexOf('?')) }
    var path = parsedUrl.pathname
    var query = parsedUrl.query
    var method = request.method

    /******** 从这里开始看，上面不要看 ************/
    const session = JSON.parse(fs.readFileSync('./db/session.json').toString())

    console.log('有个傻子发请求过来啦！路径（带查询参数）为：' + pathWithQuery)
    if (path === '/home.html'){
        const cookie = request.headers['cookie'];
        let sessionId;
        try{
            sessionId = cookie.split(';')
                .filter(s=>s.indexOf('session_id=') >= 0)[0]
                .split('=')[1];
        }catch (e) {}
        const homtHtml = fs.readFileSync('./public/home.html').toString();
        let string = '';
        if (sessionId && session[sessionId]){
            const userId = session[sessionId].user_id;
            const userAreay = JSON.parse(fs.readFileSync('./db/users.json'));
            const user = userAreay.find(user=>user.id === userId);
            if (user){
                string = homtHtml.replace('{{user.name}}',user.name).replace('{{sign_in}}','');
            }else{
                homtHtml.replace('{{user.name}}','游客')
                    .replace('{{sign_in}}',`<a href="sign_in.html">请登录</a>`);
            }
        }else{
            homtHtml.replace('{{user.name}}','游客')
                .replace('{{sign_in}}',`<a href="sign_in.html">请登录</a>`);
        }
        response.write(string);
        response.end();
    }else if (path === "/sign_in" && method === 'POST'){
        response.setHeader('Content-Type','text/html;charset=utf-8;')
        const userAreay = JSON.parse(fs.readFileSync('./db/users.json'));
        const array = [];
        request.on('data',(chunk)=>{
            array.push(chunk)
        })
        request.on('end',()=>{
            const string = Buffer.concat(array).toString();
            const param = JSON.parse(string);
            const user = userAreay.find((user)=> user.name === param.name && user.password === param.password)
            if (user === undefined){
                response.statusCode = 400;
                response.setHeader('Content-Type','text/json;charset=utf-8;')
                response.end(`{"errorCode":401}`);
            }else{
                response.statusCode = 200;
                const random = Math.random();
                session[random] = {user_id:user.id};
                fs.writeFileSync('./db/session.json',JSON.stringify(session));
                response.setHeader('Set-Cookie',`session_id=${random};HttpOnly`);
                response.end();
            }
        })
    }else if (path === '/register' && method === 'POST'){
        response.setHeader('Content-Type','text/html;charset=utf-8;')
        const userAreay = JSON.parse(fs.readFileSync('./db/users.json'));
        const array = [];
        request.on('data',(chunk)=>{
            array.push(chunk)
        })
        request.on('end',()=>{
            const string = Buffer.concat(array).toString();
            const param = JSON.parse(string);
            const lastUser = userAreay[userAreay.length-1];
            const newUser = {
                "id":lastUser ? lastUser.id+1 : 1,
                "name":param.name,
                "password":param.password
            }
            userAreay.push(newUser);
            fs.writeFileSync('./db/users.json',JSON.stringify(userAreay))
            console.log(string)
            response.end();
        })

    }else{
        response.statusCode = 200
        // 默认首页
        const filePath = path === '/' ? '/index.html' : path
        const index = filePath.lastIndexOf('.')
        // suffix 是后缀
        const suffix = filePath.substring(index)
        const fileTypes = {
            '.html':'text/html',
            '.css':'text/css',
            '.js':'text/javascript',
            '.png':'image/png',
            '.jpg':'image/jpeg'
        }
        response.setHeader('Content-Type',
            `${fileTypes[suffix] || 'text/html'};charset=utf-8`)
        let content
        try{
            content = fs.readFileSync(`./public${filePath}`)
        }catch(error){
            content = '文件不存在'
            response.statusCode = 404
        }
        response.write(content)
        response.end()
    }

    /******** 代码结束，下面不要看 ************/
})

server.listen(port)
console.log('监听 ' + port + ' 成功\n请用在空中转体720度然后用电饭煲打开 http://localhost:' + port)